package cn.thr.pethome.role.service.impl;

import cn.thr.pethome.base.query.BaseQuery;
import cn.thr.pethome.role.domain.Role;
import cn.thr.pethome.role.domain.RoleMenu;
import cn.thr.pethome.role.domain.RolePermission;
import cn.thr.pethome.role.mapper.RoleMapper;
import cn.thr.pethome.role.service.IRoleService;
import cn.thr.pethome.base.service.Impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thr
 * @since 2023-02-01
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void delete(Long id) {
        roleMapper.delMenus(id);
        roleMapper.getPermissionByRoleId(id);
        super.delete(id);
    }

    @Override
    public void update(Role role) {
        super.update(role);
        Long id = role.getId();
        roleMapper.delMenus(id);
        roleMapper.delPermission(id);

        //添加菜单的中间表
        List<Long> menus = role.getMenus(); //菜单的id
        if(!menus.isEmpty()){ //isEmpty() 如果数组没有的话 就是ture
            menus.forEach(menuId->{ //循环添加
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(role.getId()); //传入role 的id
                roleMenu.setMenuId(menuId);
                roleMapper.addByMenu(roleMenu);
            });
        }
        //添加权限的中间表
        List<Long> permissions = role.getPermissions();//权限id
        if(!permissions.isEmpty()){
            permissions.forEach(permissionId->{
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setPermissionId(permissionId);
                roleMapper.addByPermission(rolePermission);
            });
        }
    }

    @Override
    public List<Role> findByQuery(BaseQuery query) {
        List<Role> byQuery = super.findByQuery(query);
        for (Role role : byQuery) {
            Long id = role.getId();
            role.setMenuList(roleMapper.getMenusByRoleId(id));
            role.setPermissionList(roleMapper.getPermissionByRoleId(id));
        }
        return byQuery;
    }

    @Override
    public void add(Role role) {
        super.add(role);
        //添加菜单的中间表
        List<Long> menus = role.getMenus(); //菜单的id
        if(!menus.isEmpty()){ //isEmpty() 如果数组没有的话 就是ture
            menus.forEach(menuId->{ //循环添加
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(role.getId()); //传入role 的id
                roleMenu.setMenuId(menuId);
                roleMapper.addByMenu(roleMenu);
            });
        }
        //添加权限的中间表
        List<Long> permissions = role.getPermissions();//权限id
        if(!permissions.isEmpty()){
            permissions.forEach(permissionId->{
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setPermissionId(permissionId);
                roleMapper.addByPermission(rolePermission);
            });

        }
    }
}
